using System;
using System.ComponentModel;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Windows.UI.ViewManagement;

namespace App1
{
    /// <summary>
    /// 设置页面
    /// </summary>
    public sealed partial class SettingsPage : Page, INotifyPropertyChanged
    {
        private int _maxThreads = 4;
        private double _speedLimit = 0;
        private int _blurAmount = 50;

        public event EventHandler<SettingsChangedEventArgs> SettingsChanged;
        public event PropertyChangedEventHandler PropertyChanged;

        public int MaxThreads
        {
            get => _maxThreads;
            set
            {
                _maxThreads = value;
                OnPropertyChanged(nameof(MaxThreads));
                RaiseSettingsChanged();
            }
        }

        public double SpeedLimit
        {
            get => _speedLimit;
            set
            {
                _speedLimit = value;
                OnPropertyChanged(nameof(SpeedLimit));
                RaiseSettingsChanged();
            }
        }

        public int BlurAmount
        {
            get => _blurAmount;
            set
            {
                _blurAmount = value;
                OnPropertyChanged(nameof(BlurAmount));
                ApplyBlurEffect();
            }
        }

        public SettingsPage()
        {
            this.InitializeComponent();
            BlurSlider.Value = BlurAmount;
            ThreadsSlider.Value = MaxThreads;
            SpeedLimitTextBox.Text = SpeedLimit.ToString();
            BlurValueText.Text = $"{BlurAmount}%";
            ThreadsValueText.Text = MaxThreads.ToString();
        }

        private void BlurSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            BlurAmount = (int)e.NewValue;
            BlurValueText.Text = $"{BlurAmount}%";
        }

        private void ThemeRadio_Checked(object sender, RoutedEventArgs e)
        {
            if (SystemThemeRadio.IsChecked == true)
            {
                // 跟随系统主题
                var uiSettings = new UISettings();
                var color = uiSettings.GetColorValue(UIColorType.Background);
                ApplyTheme(color.R > 128 ? ElementTheme.Light : ElementTheme.Dark);
            }
            else if (LightThemeRadio.IsChecked == true)
            {
                // 浅色主题
                ApplyTheme(ElementTheme.Light);
            }
            else if (DarkThemeRadio.IsChecked == true)
            {
                // 深色主题
                ApplyTheme(ElementTheme.Dark);
            }
        }

        private void ThreadsSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            MaxThreads = (int)e.NewValue;
            ThreadsValueText.Text = MaxThreads.ToString();
        }

        private void ApplySpeedLimitButton_Click(object sender, RoutedEventArgs e)
        {
            if (double.TryParse(SpeedLimitTextBox.Text, out double speedLimit))
            {
                SpeedLimit = speedLimit;
            }
            else
            {
                // 显示错误提示
                var errorDialog = new ContentDialog
                {
                    Title = "输入错误",
                    Content = "请输入有效的数字作为速度限制",
                    CloseButtonText = "确定"
                };
                errorDialog.ShowAsync();
            }
        }

        private void ApplyTheme(ElementTheme theme)
        {
            // 应用主题到窗口
            if (Window.Current != null)
            {
                Window.Current.Content.RequestedTheme = theme;
            }
        }

        private void ApplyBlurEffect()
        {
            // 应用模糊效果到窗口背景
            if (Window.Current != null && Window.Current.Content is Grid rootGrid)
            {
                // 查找AcrylicBrush
                if (rootGrid.Background is AcrylicBrush acrylicBrush)
                {
                    // 更新模糊程度
                    acrylicBrush.TintOpacity = BlurAmount / 100.0;
                }
            }
        }

        private void RaiseSettingsChanged()
        {
            SettingsChanged?.Invoke(this, new SettingsChangedEventArgs
            {
                MaxThreads = MaxThreads,
                SpeedLimit = SpeedLimit
            });
        }

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}